<template>
    <div class="textarea-field" :class="{'van-cell--required':config.must}">
        <div class="textarea-label">{{config.label}}</div>
        <div class="textarea-value" :class="{'active':focusIdx==config.key,'error':isError==true}">
            <textarea
            v-model="config.value"
            :placeholder="config.placeholder" 
            :readonly="config.readonly"
            :disabled="config.disabled"
            :maxlength="config.maxlength"
            @focus="onFocus"
            @blur="onBlur"></textarea>
            <div class="textarea-prompt">{{config.value.length}}/{{config.maxlength}}字</div>
        </div>        
    </div>
</template>

<script>
export default {
    data() {
        return {
           focusIdx: null,
           isError: false
        };
    },
    props: ['config'],
    methods: {
        onFocus() {
            this.focusIdx = this.config.key;
        },
        onBlur() {
            this.focusIdx = null;
            if (this.config.must && this.config.value == '') {
                this.isError = true;
            } else {
                this.isError = false;
            }
        }
    },
    watch: {
        'config.value': function(val, oldVal) {
            if (val != '') {
                this.isError = false;
            }
            if (this.config.key == 'introduce') {
                this.$store.commit('SET_ABOUTUS', val);
            }
            if (this.config.key == 'dietary') {
                this.$store.commit('SET_DIETARYTABOOS', val);
            }
            if (this.config.key == 'wish') {
                this.$store.commit('SET_WISH', val);
            }
        }
    }
  };
</script>

<style>

</style>
